#pragma once
#include "cybertron/DefinesCore.hpp"
#include "Sensor/SensorCommon.pb.h"
#include <atomic>
#include <string>
#include "../../CybertronBridgeIO.hpp"
#include "cybertron/node/NodeHotAreaManager.hpp"
#include "Node/Bridge.pb.h"
using namespace cybertron;

enum EIOTaskType
{
	TaskTypeGPS = 1,
	TaskTypeTrafficLight = 2,
	TaskTypeObstacle = 3,
	TaskTypePrediction = 4,
};
class CTaskHotAreaBase
{
public:

	CTaskHotAreaBase() {}
	virtual ~CTaskHotAreaBase() {}

	void makeHotAreaBridgeHeader(int frame, Bridge::BridgeHotAreaHeader *header) {

		std::chrono::high_resolution_clock::time_point now = std::chrono::high_resolution_clock::now();
		std::chrono::nanoseconds createTime = now.time_since_epoch();
		std::chrono::milliseconds createTimeMS((createTime.count() / 1000000));

		header->set_version(1);
		header->set_timestamp((int)createTimeMS.count());
		header->set_frame(frame);

	}
	virtual void HotAreaEnter() = 0;
	virtual void HotAreaLeft(int frame, int ms, int msModNs, TrafficActorHotArea* pHotArea) = 0;
	virtual void ActorToDo(int hostVehicleId, int frame, int ms, int msModNs, TrafficActorHotArea* pHotArea, TrafficActor* pActor, int index) = 0;
	virtual char* GetResult(std::string hostVehicleId) = 0;
	virtual int GetHotAreaDataLength(std::string hostVehicleId) = 0;

	virtual void Clear() {}
	virtual EIOTaskType GetTaskType() { return mTaskType; }
	virtual int GetCommandId() { return mCommandID; }
protected:
	int mCommandID;
	EIOTaskType	mTaskType;
	Message mResultMessage;
};

class TaskHotAreaManager
{
public:
	struct MainVehicleStatus {
		int mainVehicleId;
		std::string name;
		int IsReady;
	};
	TaskHotAreaManager();
	~TaskHotAreaManager();

	void Init(CybertronBridgeIO* app);
	void HotAreaOnFrame(int frame, int ms, int msModNs);

	void OneHotAreaTaskSend(std::string hostVehicle,int frame);
	//void sendHotAreaMessage(int commanId,int mainVehicleId, int frame, const google::protobuf::MessageLite& protobufMsg);
	void sendHotAreaMessage(int commanId, int mainVehicleId, int frame, char* pBuffer, int length);

	void OneHotAreaTaskEnter();

	void AddMainVehicleID(int mainVehicleId) {
		MainVehicleStatus mainVehicleStatus;
		mainVehicleStatus.mainVehicleId = mainVehicleId;
		mainVehicleStatus.name = "";
		mainVehicleStatus.IsReady = 0;
		mMainVehicleMap[mainVehicleId] = mainVehicleStatus;
	}
	MainVehicleStatus* GetMainVehicleStatus(int mainVehicleId) {
		auto it = mMainVehicleMap.find(mainVehicleId);
		if (it == mMainVehicleMap.end()) {
			return 0;
		}
		return &it->second;
	}
	void SendMainVehicleList(SocketBasePtr pSender);
	static TaskHotAreaManager& getInstance();
protected:

	typedef  std::map<EIOTaskType, CTaskHotAreaBase*> TaskHotAreaMap;
	typedef  std::map<int, MainVehicleStatus> MainVehicleMap;
	TaskHotAreaMap mHotAreaTaskMap;
	MainVehicleMap mMainVehicleMap;
	
	//NodeHotAreaManager* mpNodeHotAreaManager;
	CybertronBridgeIO* mpApp;
	bool mbIsSendConfig;
	int mLightIndex;
};

